import {Injectable} from '@angular/core';
import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest} from '@angular/common/http';
import {Observable} from 'rxjs/Observable';

@Injectable()
export class AuthInterceptor implements HttpInterceptor {
    constructor() {
    }

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        // Get the auth header from the service.
        const authHeader = this.getAuthorizationHeader();
        // Clone the request to add the new header.
        console.log(authHeader + 'header===');
        let authReq;
        if (authHeader.includes('undefined'))
            authReq = req.clone();
        else
            authReq = req.clone({headers: req.headers.set('Authorization', authHeader)});
        // Pass on the cloned request instead of the original request.
        return next.handle(authReq);
    }

    getAuthorizationHeader() {
        return localStorage.hasOwnProperty('token') ? localStorage.getItem('token') : '';
    }
}
